// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BackupViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the BackupViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client.ViewModel
{
    using System.Data.EntityClient;
    using System.IO;
    using System.Windows.Input;
    using Common;
    using Hisaab.Common;

    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Windows.Client.ViewModel.BackupViewModel"/> type.
    /// </summary>
    public class BackupViewModel : DialogViewModel
    {
        /// <summary>
        /// Backing field for backup path
        /// </summary>
        private string _backupPath;

        /// <summary>
        /// Query delegate command
        /// </summary>
        private DelegateCommand _backupCommand;

        /// <summary>
        /// Gets the new tag command.
        /// </summary>
        public ICommand BackupCommand
        {
            get
            {
                if (_backupCommand == null)
                {
                    _backupCommand = new DelegateCommand(ExecuteBackup, CanExecuteBackup);
                }

                return _backupCommand;
            }
        }

        /// <summary>
        /// Gets or sets the backup path.
        /// </summary>
        /// <value>
        /// The backup path.
        /// </value>
        public string BackupPath
        {
            get
            {
                return _backupPath;
            }

            set
            {
                if (_backupPath != value)
                {
                    _backupPath = value;

                    OnPropertyChanged("BackupPath");
                }
            }
        }

        /// <summary>
        /// Executes the backup.
        /// </summary>
        public void ExecuteBackup()
        {
            //try
            //{
            //    var connectionString = GetConnectionSetting();

            //    var serverConn = new ServerConnection { ConnectionString = connectionString };
            //    var sqlsrv = new Server(serverConn);

            //    var bkpDatabase = new Backup();
            //    bkpDatabase.Action = BackupActionType.Database;
            //    bkpDatabase.Database = "Hisaab";

            //    var bkpDevice = new BackupDeviceItem(BackupPath, DeviceType.File);
            //    bkpDatabase.Devices.Add(bkpDevice);
            //    bkpDatabase.SqlBackup(sqlsrv);

            //    if (!File.Exists(BackupPath))
            //    {
            //        throw new ApplicationException();
            //    }
            //}
            //catch (Exception ex)
            //{
            //}
        }

        /// <summary>
        /// Gets the connection setting.
        /// </summary>
        /// <returns>Database connection settings</returns>
        public string GetConnectionSetting()
        {
            var connectionString = ServiceLocator.Current.GetInstance<IConfiguration>().ConnectionStrings["HisaabDataModel"].ConnectionString;

            var connectionStringBuilder = new EntityConnectionStringBuilder(connectionString);
            return connectionStringBuilder.ConnectionString;
        }

        /// <summary>
        /// Determines whether this instance [can execute backup].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance [can execute backup]; otherwise, <c>false</c>.
        /// </returns>
        public bool CanExecuteBackup()
        {
            return !string.IsNullOrWhiteSpace(BackupPath) && Directory.Exists(Path.GetDirectoryName(BackupPath));
        }
    }
}